package com.spr.application;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.webkit.CookieManager;
import android.webkit.CookieSyncManager;
import android.webkit.JavascriptInterface;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;

import com.google.zxing.integration.android.IntentIntegrator;
import com.google.zxing.integration.android.IntentResult;
import com.spr.config.WebConfig;

public class WebActivityBack extends AppCompatActivity {

    private WebView webView;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_web);

        webView = (WebView)this.findViewById(R.id.webBrowser);

        webView.getSettings().setJavaScriptEnabled(true);
        webView.addJavascriptInterface(new WebOPeration(this), "client");

        syncCookie(this);

        webView.loadUrl(WebConfig.EJQ_DOMIN + WebConfig.ORDER_PATH);
        //webView.getSettings().
        webView.setWebViewClient(new WebViewClient(){
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);
                return super.shouldOverrideUrlLoading(view, url);
            }
        });

    }

    // 设置cookie
    public static void syncCookie(Context context) {
        CookieSyncManager.createInstance(context);
        CookieManager cookieManager = CookieManager.getInstance();
        cookieManager.setAcceptCookie(true);
        cookieManager.removeSessionCookie();//移除
        cookieManager.setCookie(WebConfig.EJQ_DOMIN , LoginActivity.cookieVal);//cookies是在HttpClient中获得的cookie
        CookieSyncManager.getInstance().sync();
    }

    @Override
// 通过 onActivityResult的方法获取 扫描回来的 值
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        IntentResult intentResult = IntentIntegrator.parseActivityResult(requestCode,resultCode,data);
        if(intentResult != null) {
            if(intentResult.getContents() == null) {
              //  Toast.makeText(this,"内容为空",Toast.LENGTH_LONG).show();
            } else {
                Toast.makeText(this,"扫描成功",Toast.LENGTH_LONG).show();
                // ScanResult 为 获取到的字符串
                String sanResult = intentResult.getContents();

                webView.loadUrl(sanResult);
            }
        } else {
            super.onActivityResult(requestCode,resultCode,data);
        }
    }

    public static class WebOPeration{

       private  Activity mActivity;

        public WebOPeration(Activity mActivity) {
            this.mActivity = mActivity;
        }

        @JavascriptInterface
        public void logout() {


            Intent intent = new Intent(mActivity, LoginActivity.class);
            intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
            mActivity.startActivity(intent);

        }

        @JavascriptInterface
        public void openScan() {

           // Toast.makeText(mActivity,"openScan",Toast.LENGTH_SHORT).show();

            new IntentIntegrator(mActivity)
                    .setOrientationLocked(false)
                    .setCaptureActivity(CustomScanActivity.class) // 设置自定义的activity是CustomActivity
                    .initiateScan(); // 初始化扫描
        }

    }

}
